package com.wh.influxdb.common.utils;

import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

import static com.wh.influxdb.common.utils.InfluxdbUtil.NANOSECONDS_PER_SECOND;


public class TimeUtil {

    public static final long NANOS_PER_SECOND = 1000_000_000L;

    public static long timeToNanos(LocalDateTime localDateTime){
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return BigInteger.valueOf(instant.getEpochSecond())
                .multiply(BigInteger.valueOf(NANOS_PER_SECOND))
                .add(BigInteger.valueOf(instant.getNano())).longValue();
    }

    public static LocalDateTime nanosToTime(long nanos){
        Instant instant = Instant.ofEpochSecond(nanos / NANOSECONDS_PER_SECOND.longValue(), (int) (nanos % NANOSECONDS_PER_SECOND.longValue()));
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

}
